package com.xingzhi.common.util;

import java.security.MessageDigest;
import java.util.Random;
import java.util.UUID;

/**
 * Created by daniel.luo on 2018/5/23.
 */
public class HashUtil {
    public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
        "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
        "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
        "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
        "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
        "W", "X", "Y", "Z" };


    public static String getUUID(){
        UUID uuid=UUID.randomUUID();
        String str = uuid.toString();
        String uuidStr=str.replace("-", "");
        return uuidStr;
    }

    public static String generateShortUuid() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();

    }

    /**
     * 用MD5加密内容，生成md5码
     *
     * @param
     * @return
     */
    public static String EncodeByMD5(String strs) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            byte[] btInput = strs.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 生成用户行为的流水单号
     *
     * @param uid  用户uid
     * @param type 1新建管理员,2添加课件素材
     * @return
     */
    public static String generateSerialNum(String uid, String type) {
        String ts = TimeUtil.getTimeStamp();
        return EncodeByMD5(uid + ts + type);
    }
    /**
     * 生成guid
     *
     * @param name
     * @return
     */
    public static String generateSerial(String name) {
        String ts = TimeUtil.getTimeStamp();
        return EncodeByMD5(name + ts);
    }
    /**
     * 生成长度为size的随机数
     *
     * @return
     */
    public static String generateRandomStr(int size) {
        try {
            if (size > 32) throw new Exception("size的长度不能超过32..");
            int randomNum = new Random().nextInt(999);
            String md5Str = EncodeByMD5(randomNum + TimeUtil.getTimeStamp());
            return md5Str.substring(0, size);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 生成长度为size的，0-9的随机数
     *
     * @param size
     * @return
     */
    public static String generateRandomNum(int size) throws Exception {
        if (size > 32) throw new Exception("size的长度不能超过32..");
        String randomRet = "";
        for (int i = 0; i < size; i++) {
            int num = new Random().nextInt(10);
            randomRet += String.valueOf(num);
        }
        return randomRet;
    }

    public static void main(String[] args) {
        System.out.println(generateSerialNum("18770076907", "0"));
    }

}
